<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('生成学号')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-studentNo-add">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 ><span th:text="${year}" id="year"></span>届学号生成 <small>共<span th:text="${studentCount}" id="studentCount"></span>个新生  分<span th:if="${not #lists.isEmpty(classList)}" th:text="${classList.size()}" id="classCount"></span>个班级</small></h3>
                    <span id="helpBlock" class="help-block" style="color: red;">生成规则:固定前缀+班级代码+3位补码  (补码按照学生分数从高到低排序,位数不足的补0) </span>
                    <span class="help-block" style="color: red;">示例:固定前缀:130722 班级代码:001 学生分数最高的为1 补位生成001 最终【130722001001】</span>
                </div>
                <div class="panel-body">

                    <div class="form-group">
                        <label class="col-sm-2 control-label is-required">入学年份：</label>
                        <div class="date col-sm-8">
                            <input id="comeYear" name="comeYear" th:value="${year}" class="form-control" placeholder="请选择入学年份" type="text">
                        </div>

                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label is-required">固定前缀：</label>
                        <div class="col-sm-8 ">
                            <input name="prefix"  type="number" min="0" class="form-control" required id="prefix">
                        </div>

                    </div>


                        <div class="form-group">
                            <label class="col-sm-2 control-label is-required" >班级代码：</label>

                            <div class="col-sm-8 " id="classList">
                                <div th:each="class:${classList}">
                                    <div class="input-group m-b"><span class="input-group-addon className" th:text="${class.className}"></span>
                                        <input  class="classId"  type="hidden" th:value="${class.id}">
                                        <input   type="number" max="9999999" min="0" class="studentNoBatch form-control" required>
                                    </div>
                                </div>
                            </div>

                        </div>


                </div>

            </div>
            <div class="row">
                <div class="col-sm-offset-5 col-sm-10">
                    <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>生 成</button>&nbsp;
                    <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script type="text/javascript">
        var prefix = ctx + "school/student"
        $("#form-studentNo-add").validate({
            focusCleanup: true
        });

        $("input[name='comeYear']").datetimepicker({
            format: "yyyy",
            autoclose: true,
            startView:4,
            minView:4,
            todayHighlight:true
        });

        $('#comeYear').datetimepicker()
            .on('changeYear', function(ev){
                if (ev.date != undefined){

                    var date = new Date(ev.date);
                    $("#year").text(date.getFullYear());
                    $.get(prefix + "/getClassListByComeYear?comeYear="+date.getFullYear(),function (data) {
                        var str = '';
                        if(data && JSON.stringify(data)!='{}'){
                            $("#studentCount").text(data.studentCount);
                            $("#classCount").text(data.classList.length);
                            for (let i = 0; i < data.classList.length; i++) {
                                str +='<div class="input-group m-b"><span class="input-group-addon className">'+data.classList[i].className+'</span>\n' +
                                    '                                        <input  class="classId"  type="hidden" value="'+data.classList[i].id+'">\n' +
                                    '                                        <input   type="number" max="9999999" min="0" class="studentNoBatch form-control" required>\n' +
                                    '                                    </div>';
                            }
                            $("#classList").html(str);

                        }else {
                            $("#studentCount").text(0);
                            $("#classCount").text(0);
                            $("#classList").html(str);
                        }
                    })
                }
            });
        function submitHandler() {

            if($("#studentCount").text() == 0){
                $.modal.alertError("所选年份没有学生!");
                return;
            }
            if ($.validate.form()) {
                var data = {};
                data["prefix"]=$("#prefix").val();
                var studentNos = $('.studentNoBatch');
                var classIds = $('.classId');

                for (let i = 0; i < studentNos.length; i++) {
                    data["studentNoRequestList["+i+"].classId"]=classIds[i].value;
                    data["studentNoRequestList["+i+"].studentBatchNo"]=studentNos[i].value;

                }

                $.operate.save(prefix + "/createStudentNoSave", data);
            }
        }





    </script>
</body>
</html>